package com.itron.base.util;



import com.itron.base.service.BaseService;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;


/**
 * 邮件发送相关的工具类
 */

public class MailUtil {

    private static JavaMailSender javaMailSender;

    private static String sendName;

    static {
        javaMailSender = SpringContextUtils.getBean(JavaMailSender.class);
        BaseService bean = SpringContextUtils.getBean(BaseService.class);
        sendName= bean.sendName;
    }

    public static SimpleMailMessage getSimpleMailMessage() {
        return new SimpleMailMessage();
    }

    public static boolean sendSimpleMail(String mail, String text) {
        try {
            System.out.println(sendName);
            System.out.println(text);
            System.out.println(mail);
            SimpleMailMessage simpleMail = getSimpleMailMessage();
            simpleMail.setFrom(sendName);
            simpleMail.setTo(mail);
            simpleMail.setText(text);
            javaMailSender.send(simpleMail);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
